package com.ady.collect;

import com.ady.util.FrameUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <b>description</b>： <br>
 * <b>time</b>：2018-06-22 17:09 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class Demo1 {

    static String docDir = "E:\\myself\\idea\\ady\\doc";

    public static void main(String[] args) throws IOException {
        m1();
        m2();
        m3();
    }

    public static void m3() throws IOException {
        String s = FileUtils.readFileToString(new File(docDir + "\\film\\dytt.json"));
        JSONObject jsonObject = JSON.parseObject(s);
        List<Map<String, String>> list = FrameUtil.newArrayList();
        for (String title : jsonObject.keySet()) {
            String string = jsonObject.getString(title);
            FilmModel filmModel = JSON.parseObject(string, FilmModel.class);
            list.add(FrameUtil.newHashMap(filmModel.getId(), filmModel.getTitle()));
        }
        String out = "var filmIndexMap = " + FrameUtil.json(list) + ";";
        FileUtils.writeStringToFile(new File(docDir + "\\html\\film\\index.js"), out);
    }


    public static void m2() throws IOException {
        File filmDir = new File(docDir + "\\html\\film\\new");
        File[] files = filmDir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.indexOf("dytt_") != -1;
            }
        });
        DyttCollect pageProcessor = new DyttCollect();
        for (File file : files) {
            String s = FileUtils.readFileToString(file);
            JSONObject jsonObject = JSON.parseObject(s);
            for (String title : jsonObject.keySet()) {
                String string = jsonObject.getString(title);
                FilmModel filmModel = JSON.parseObject(string, FilmModel.class);
                filmModel.initExtMap();
                System.out.println(string);
                pageProcessor.dataMap1.put(title, filmModel);
                String json1 = FrameUtil.json(pageProcessor.dataMap1, true);
                FileUtils.writeStringToFile(new File(docDir + "\\html\\film\\detail", filmModel.getId() + ".json"), json1);
                pageProcessor.dataMap1.clear();
            }
        }

    }

    public static void m1() throws IOException {
        File filmDir = new File(docDir + "\\html\\film\\new");
        File[] files = filmDir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.indexOf("dytt_") != -1;
            }
        });
        DyttCollect pageProcessor = new DyttCollect();
        for (File file : files) {
            String s = FileUtils.readFileToString(file);
            JSONObject jsonObject = JSON.parseObject(s);
            for (String title : jsonObject.keySet()) {
                try {
                    String string = jsonObject.getString(title);
                    FilmModel filmModel = JSON.parseObject(string, FilmModel.class);
                    filmModel.initExtMap();
                    pageProcessor.dataMap.put(title, filmModel);
                    pageProcessor.dataMap1.put(title, filmModel);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(title);
                    System.out.println(file.getAbsolutePath());
                }
            }
            String json = FrameUtil.json(pageProcessor.dataMap, true);
            FileUtils.writeStringToFile(new File(docDir + "\\film\\dytt.json"), json);
            String json1 = FrameUtil.json(pageProcessor.dataMap1, true);
            FileUtils.writeStringToFile(file, json1);
            pageProcessor.dataMap1.clear();
        }

    }
}
